package beans;

import entities.Categorie;
import entities.Comment;
import entities.DVD;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class DVDFacade extends AbstractFacade<DVD> {

    @PersistenceContext(unitName = "ProjetAppliWebPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DVDFacade() {
        super(DVD.class);
    }

    public DVD getByTitleAndCategory(String title, Categorie categorie) {
        Query query = em.createQuery("SELECT d FROM DVD d WHERE d.title = :title AND d.categorie.name = :name");
        query.setParameter("title", title);
        query.setParameter("name", categorie.getName());

        try {
            return (DVD) query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    public DVD create(String title, Categorie categorie) {
        DVD dvd = getByTitleAndCategory(title, categorie);

        if (dvd == null) {
            dvd = new DVD(title, categorie);
            create(dvd);
            return dvd;
        }

        return null;
    }

    public Collection<DVD> getAll() {
        Query query = em.createQuery("SELECT d FROM DVD d ORDER BY d.title, d.categorie.name");
        return query.getResultList();
    }

    public Collection<DVD> getLast(int max) {
        Query query = em.createQuery("SELECT d FROM DVD d ORDER BY d.releaseDate DESC");
        query.setMaxResults(max);
        return query.getResultList();
    }
    
    public void addComment(Comment comment, DVD dvd) {
        dvd.getCommentaires().add(comment);
        edit(dvd);
    }
    
    public int countByCategory(Categorie category) {
        Query query = em.createQuery("SELECT COUNT(d) FROM DVD d WHERE d.categorie.id = :id");
        query.setParameter("id", category.getId());
        return Integer.valueOf(query.getSingleResult().toString());
    }
    
    public Collection<DVD> getByCategory(Categorie category, int first, int max) {
        return getByCategory(category, first, max, "releaseDate", "DESC");
    }
    
    public Collection<DVD> getByCategory(Categorie category, int first, int max, String sort, String value) {
        Query query = em.createQuery("SELECT d FROM DVD d WHERE d.categorie.id = :id ORDER BY d." + sort + " " + value);
        query.setParameter("id", category.getId());
        query.setFirstResult(first);
        query.setMaxResults(max);
        return query.getResultList();
    }
    
    //bes
    public Collection<DVD> getByKeyword(String keyword) {
        Query query = em.createQuery("SELECT d FROM DVD d WHERE LOWER(d.title) LIKE :keyword ORDER BY d.title");
        query.setParameter("keyword", "%" + keyword.toLowerCase() + "%");
        return query.getResultList();
    }

    public Collection<DVD> getByCategoryKeyword(String keyword) {
        Query query = em.createQuery("SELECT d FROM DVD d WHERE LOWER(d.categorie.name) LIKE :keyword ORDER BY d.title");
        query.setParameter("keyword", "%" + keyword.toLowerCase() + "%");
        return query.getResultList();
    }
    
     public Collection<Object> getByPublisherKeyword(String keyword, boolean all) {
        Query query;

        if (all) {
            query = em.createQuery("SELECT d FROM DVD d WHERE LOWER(d.publisher) LIKE :keyword ORDER BY d.title");
        } else {
            query = em.createQuery("SELECT DISTINCT d.publisher FROM DVD d WHERE LOWER(d.publisher) LIKE :keyword ORDER BY d.publisher");
        }

        query.setParameter("keyword", "%" + keyword.toLowerCase() + "%");
        return query.getResultList();
    }

    public Collection<String> getTitles() {
        Query q = em.createQuery("SELECT DISTINCT d.title FROM DVD d ORDER BY d.title");
        return q.getResultList();
    }

    public Collection<String> getPublishers() {
        Query q = em.createQuery("SELECT DISTINCT d.publisher FROM DVD d ORDER BY d.publisher");
        return q.getResultList();
    }

    
}
